/**
 * CommandOptions.
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.helper;

import java.util.ArrayList;

/**
 * Helper class to parse and manage command line arguents.
 *
 */
public class CommandOptions
{
	protected ArrayList<String> arguments;
	
	/**
	 * 
	 * @param args String[]
	 */
	public CommandOptions(String[] args)
	{
		parse(args);
	}
	
	/**
	 * Parse the command line arguments.
	 * 
	 * @param args String[]
	 */
	public void parse(String[] args)
	{
		arguments = new ArrayList<String>();
		for ( int i = 0; i < args.length; i++ ) {
			arguments.add(args[i].trim());
		}
	}
	
	/**
	 * Parse the command line arguments.
	 * 
	 * @param args String[]
	 * @param separator String
	 */
	public void parse(String[] args, String separator)
	{
		arguments = new ArrayList<String>();
		for ( int i = 0; i < args.length; i++ ) {
			String[] tok = StringHelper.tokenize2StringArray(args[i].trim(), separator);
			
			arguments.add(tok[0]);
			arguments.add(tok[1]);
		}
	}
	
	/**
	 * Get the count of arguments.
	 * 
	 * @return int size
	 */
	public int size()
	{
		return arguments.size();
	}
	
	/**
	 * 
	 * @param option
	 * @return
	 */
	public boolean hasOption(String option)
	{
		boolean hasValue = false;
		
		String str;
		for ( int i = 0; i < arguments.size(); i++ ) {
			str = (String)arguments.get(i);
			if ( true == str.equalsIgnoreCase(option) ) {
				hasValue = true;
				break;
			}
		}
		
		return hasValue;
	}
	
	/**
	 * 
	 * @param options String
	 * @param sep String
	 * 
	 * @return boolean
	 */
	public boolean hasOption(String options, String sep)
	{
		boolean hasValue = false;
		String str;
		
		String[] tok = StringHelper.tokenize2StringArray(options, sep);
		
		for( int j = 0; j < tok.length && hasValue == false; j++ ) {
			for ( int i = 0; i < arguments.size(); i++ ) {
				str = (String)arguments.get(i);
				if ( true == str.equalsIgnoreCase(tok[j]) ) {
					hasValue = true;
					break;
				}
			}
		}
		
		return hasValue;
	}
	
	/**
	 * 
	 * @param option String
	 * 
	 * @return String
	 */
	public String valueOf(String option)
	{
		String value = null;
		
		String str;
		for ( int i = 0; i < arguments.size(); i++ ) {
			str = (String)arguments.get(i);
			if ( true == str.equalsIgnoreCase(option) ) {
				value = (String)arguments.get(i+1);
				break;
			}
		}
		
		return value;
	}
	
	/**
	 * 
	 * @param options String
	 * @param sep String
	 * 
	 * @return String
	 */
	public String valueOf(String options, String sep)
	{
		String value = null;
		String str;
		
		String[] tok = StringHelper.tokenize2StringArray(options, sep);
		
		for( int j = 0; j < tok.length && value == null; j++ ) {
			for ( int i = 0; i < arguments.size(); i++ ) {
				str = (String)arguments.get(i);
				if ( true == str.equalsIgnoreCase(tok[j]) ) {
					value = (String)arguments.get(i+1);
					break;
				}
			}
		}
		
		return value;
	}
	
	/*
	public static void main(String[] args)
	{
		CommandOptions cmd = new CommandOptions(args);
		
		System.out.println(cmd.hasOption("-value"));
		System.out.println(cmd.hasOption("-junk"));
		System.out.println(cmd.valueOf("-value"));
		System.out.println(cmd.valueOf("-junk"));
	}
	*/
}

/* end */
